package com.io.model;

import com.io.protocol.LengthConstants;
import io.netty.buffer.ByteBuf;

public class BaseModel {


    protected int command;
    protected int tag;

    public int getCommand() {
        return command;
    }

    public void setCommand(int command) {
        this.command = command;
    }

    public int getTag() {
        return tag;
    }

    public void setTag(int tag) {
        this.tag = tag;
    }

    //向客户端发送的类需要override此方法,计算正文长度（哪里算正文？看文档）
    protected int calculateMainBodyLength(){
        return 0;
    }

    //向客户端发送的类需要override此方法,将正文内容写入ByteBuf中（哪里算正文？看文档）
    protected void writeMainBodyToByteBuff(ByteBuf buff){
    }

    //向客户端输出内容,除了最后两字节的CRC外
    public void writeToByteBuff(ByteBuf buff){
        buff.writeByte(LengthConstants.ThePackageHeader);
        buff.writeShort(LengthConstants.TagLength + LengthConstants.CommandLength + calculateMainBodyLength()+ LengthConstants.CrcLength);
        buff.writeShort(tag);
        buff.writeShort(command);
        writeMainBodyToByteBuff(buff);
    }
}
